package com.bw.lib_core.mvvm.model;

import com.blankj.utilcode.util.GsonUtils;
import com.bw.lib_core.bean.BaseBean;
import com.bw.lib_core.bean.ErrorBean;
import com.bw.lib_core.network.RxFunction;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public
class BaseModel implements IModel{

    @Override
    public RequestBody createJsonBody(Map<String, Object> values) {
        if (values == null){
            RequestBody.create(MediaType.parse("application/json"),"");
        }
        return RequestBody.create(MediaType.parse("application/json"), GsonUtils.toJson(values));
    }

    /**
     * 数据层提供转换方法
     */
    @Override
    public Observable<BaseBean> observableMap(Observable<?> observable) {
        return observable.map(new RxFunction());
    }

    @Override
    public Observable<BaseBean> observableMerge(Observable... observable) {
        ErrorBean errorBean = new ErrorBean();

        if (observable == null){
            return Observable.just(errorBean);
        }
        if (observable.length == 0){
            return Observable.just(errorBean);
        }
        List<Observable<BaseBean>> list = new ArrayList<>();
        for (int i = 0; i < observable.length; i++) {
            list.add(observable[i]);
        }
        return Observable.merge(list);
    }


}
